package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Log;
import com.tanhua.dubbo.server.pojo.UserFreeze;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
public class UserFreezeApiImpl implements UserFreezeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean freezeUser(UserFreeze userFreeze) {
        try {

            mongoTemplate.save(userFreeze);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean unfreezeUser(Integer userId, String reasonsForThawing) {
        try {
            UserFreeze userFreeze = new UserFreeze();
            userFreeze.setUserId(userId);
            userFreeze.setReasonsForThawing(reasonsForThawing);
            userFreeze.setLogTime(System.currentTimeMillis());
            mongoTemplate.save(userFreeze);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * @param userId
     * @return ture 冻结
     */
    @Override
    public Boolean findFreezeStatue(Integer userId) {

        Query query = Query.query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("logTime")));

        UserFreeze one = mongoTemplate.findOne(query, UserFreeze.class);

        if (null == one) {
            return false;
        }
        return null != one.getFreezingTime() && one.getFreezingTime() >= System.currentTimeMillis();
    }

    @Override
    public Map<Integer, String> findFreezeStatueByIds(List<Integer> ids) {
        Query query = Query.query(Criteria.where("userId").in(ids)).with(Sort.by(Sort.Order.asc("logTime")));
        List<UserFreeze> freezeList = mongoTemplate.find(query, UserFreeze.class);
        HashMap<Integer, String> map = new HashMap<>();

        for (UserFreeze userFreeze : freezeList) {
            //冻结 2
            Long freezingTime = userFreeze.getFreezingTime();
            if (null != freezingTime && freezingTime >= System.currentTimeMillis()) {
                map.put(userFreeze.getUserId(), "2");
            } else {
                map.put(userFreeze.getUserId(), "1");
            }
        }
        return map;
    }

    @Override
    public Map<Integer, Long> findUserLastTimeByIds(List<Integer> ids) {
        //得到所有的日志时间
        Query query = Query.query(Criteria.where("userId").in(ids)).with(Sort.by(Sort.Order.asc("logTime")));
        List<Log> logs = mongoTemplate.find(query, Log.class);
        HashMap<Integer, Long> lastTime = new HashMap<>();

        //遍历所有的日志
        for (Log log : logs) {
            //针对每一条日志做处理
            Integer userId = log.getUserId();
            lastTime.put(userId, log.getLogTime());
        }

        return lastTime;
    }
}
